package com.powernode.springmvc.controller;

import com.powernode.springmvc.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@Controller
public class UserController {
    @RequestMapping(value = "/")
    public String toRegister() {
        // 返回逻辑视图
        return "register";
    }

    // @RequestMapping(value = "/user/reg",method = RequestMethod.POST)
    /*@PostMapping("/user/reg")
    public String register(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        // HttpServletRequest,HttpServletResponse,HttpSession 属于原生的Servlet-API
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);


        // 获取请求提交的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String[] interests = request.getParameterValues("interest");
        String intro = request.getParameter("intro");

        System.out.println(username);
        System.out.println(password);
        System.out.println(sex);
        System.out.println(Arrays.toString(interests));
        System.out.println(intro);

        return "ok";
    }*/

    /*@PostMapping("/user/reg")
    public String register(
            //@RequestParam(value = "username")// value可以，name也可以
            //@RequestParam(name = "username")// username不能随便写，最好是复制过来的
            @RequestParam(name = "uname",required = false)// 如果前端没有提供 uname 参数，报错误：400
            String a, // 变量名随意
            @RequestParam(name = "password")
            String password,
            @RequestParam("sex")
            Integer sex,// SpringMVC可以自动帮我们做类型转换，从前端提交的'0'/'1'字符串，可以转换成Integer类型
            @RequestParam("interest")
            String[] interests,
            @RequestParam("intro")
            String intro,
            @RequestParam(value = "age",required = false,defaultValue = "20")
            Integer age) {

        System.out.println(a);
        System.out.println(password);
        System.out.println(sex);
        System.out.println(Arrays.toString(interests));
        System.out.println(intro);
        System.out.println(age);

        return "ok";
    }*/

    /*@PostMapping("/user/reg")
    public String register(String uname, String password, Integer sex, String[] interest, String intro, Integer age) {
        System.out.println(uname);
        System.out.println(password);
        System.out.println(sex);
        System.out.println(Arrays.toString(interest));
        System.out.println(intro);
        System.out.println(age);

        return "ok";
    }*/

    /*@PostMapping("/user/reg")
    public String register(String uname, String password, Integer sex, String interest, String intro, Integer age) {
        System.out.println(uname);
        System.out.println(password);
        System.out.println(sex);
        System.out.println(interest);
        System.out.println(intro);
        System.out.println(age);

        return "ok";
    }*/

    /*@RequestMapping("/user/reg")
    public String register(User user,
                           @RequestHeader(value = "Referer",required = false,defaultValue = "")
                           String referer,
                           @RequestHeader(value = "Host",required = false,defaultValue = "")
                           String host,
                           @CookieValue(value = "id",required = false,defaultValue = "")
                           String cookie) {
        System.out.println(user);
        System.out.println(referer);
        System.out.println(host);
        System.out.println(cookie);
        return "ok";
    }*/

    @RequestMapping("/user/reg")
    public String register(User user,HttpServletRequest request) throws UnsupportedEncodingException {
        // 设置请求体的字符编码方式，解决post请求乱码问题
        //request.setCharacterEncoding("UTF-8");

        System.out.println(user);
        return "ok";
    }
}
